home *** CD-ROM | disk | FTP | other *** search
- /* FileDisplay - maintain a TERec of what's on the screen from a long file
- messages:
- FD_New Create a new record, return handle in (FDHandle) *arg1
- Read from file (LFile *) arg2
- To be displayed in current window, inside
- (Rect *) arg3
- Tabs set to (long) arg4
- Reads initial part of file
- Can assume starting location is 0
-
- All of the rest take an FDHandle as arg1; if it's NULL, nothing is done
-
- FD_Redraw Read from line at offset (long) arg3 from one
- containing char (long *) arg2,
- redraw the screen, return in arg2 the actual start
-
- FD_Scroll Scroll (long) arg2 pixels right & (long) arg3 lines
- down (left or up if negative) (redraws screen)
- returns starting char# in (long *) arg4
- >>> arg3 should be either -1, 0, or +1 <<<
- (this is enforced by the routine)
-
- FD_Resize Window has been resized, so update data
- (Rect *) arg2 has rectangle to show text in
-
- FD_SetTabs Set tabs to (long) arg2
-
- FD_Update Handle update event (caller has called beginupdate)
-
- FD_Click Handle mousedown events
- (Event *) arg2 is the event
-
- FD_Dispose Dispose of the record
-
- FD_Copy Copy selection to clipboard
-
- FD_Activate Activate or deactivate record in response to activate
- event
- (long) arg2 is nonzero iff activate
-
- FD_Idle Call often to make caret blink
- Must use this, not teidle, to get tabs handled correctly
-
- Needs an external routine, GenError( str1, str2, str3, str4 ); whose
- arguments are Pascal strings
- */
-
- #include <Quickdraw.h>
- #include <ScrapMgr.h>
- #include <TextEdit.h>
- #include <FileMgr.h>
- #include <EventMgr.h>
- #include <strings.h>
- #include "LineFile.h"
- #include "FileDisplay.h"
-
- #define NULL 0L
-
- #define MAXLINES(H) ( (**(H)).viewRect.bottom - (**(H)).viewRect.top ) \
- / (**(H)).lineHeight
-
- #define ALLOCSIZE 1000L
-
- /* the following globals are used by call-back procedures: */
-
- static ProcPtr report_proc;
- static FDHandle theFD;
- static QDProcs lowqdprocs;
- static int tab_size;
-
- void New();
- void Redraw();
- void Scroll();
- void Resize();
- void SetTabs();
- void Update();
- void Click();
- void Dispose();
- void Copy();
- void Activate();
- void Rec_Activate();
- void Idle();
-
- void FileDisplay( message, hFD, arg2, arg3, arg4 )
- int message;
- register long hFD, arg2, arg3, arg4;
- {
- if ( message != FD_New )
- if ( hFD == NULL )
- return;
- else
- patchtabs( hFD );
-
- switch ( message ) {
- case FD_New:
- New( hFD, arg2, arg3, arg4 );
- break;
-
- case FD_Redraw:
- Redraw( hFD, arg2, arg3 );
- break;
-
- case FD_Scroll:
- Scroll( hFD, arg2, arg3, arg4 );
- break;
-
- case FD_Resize:
- Resize( hFD, arg2 );
- break;
-
- case FD_SetTabs:
- SetTabs( hFD, arg2 );
- break;
-
- case FD_Update:
- Update( hFD );
- break;
-
- case FD_Click:
- Click( hFD, arg2 );
- break;
-
- case FD_Dispose:
- Dispose( hFD );
- break;
-
- case FD_Copy:
- Copy( hFD );
- break;
-
- case FD_Activate:
- Activate( hFD, arg2 );
-
- case FD_Idle:
- Idle( hFD );
- }
-
- if ( message != FD_Dispose && message != FD_New )
- unpatchtabs( hFD );
- }
-
- Boolean memok()
- {
- register int err;
- char errs[256];
-
- if ( ( err = MemError() ) != noErr ) {
- NumToString( (long) err, errs );
- GenError( "\pMemory Error ", errs, "\p", "\p" );
- return FALSE;
- }
-
- return TRUE;
- }
-
- static Boolean sel_in_view( hFD )
- register FDHandle hFD;
- {
- register long first, last;
-
- if ( (**hFD).actuallines == 0 ) return FALSE;
-
- first = (**hFD).filestarts[ 0 ];
- last = first + (**(**hFD).TE).teLength;
-
- return ( (**hFD).sel_start < last && (**hFD).sel_end > first ) ||
- ( (**hFD).sel_start == first && (**hFD).sel_end == first );
- }
-
- static void set_te_select( hFD )
- register FDHandle hFD;
- {
- register long first, length, selstart, selend;
- register TEHandle hTE = (**hFD).TE;
-
- if ( (**hFD).actuallines == 0 ) return;
-
- first = (**hFD).filestarts[ 0 ];
- length = (**hTE).teLength;
-
- selstart = (**hFD).sel_start - first;
- selend = (**hFD).sel_end - first;
-
- if ( selstart < 0L )
- selstart = 0L;
- else if ( selstart > length )
- selstart = length;
-
- if ( selend < 0L )
- selend = 0L;
- else if ( selend > length )
- selend = length;
-
- (**hTE).selStart = selstart;
- (**hTE).selEnd = selend;
- }
-
- static void FillFD( hFD, pos, offset )
- register FDHandle hFD;
- long pos, offset;
- {
- register int i,
- max = (**hFD).maxlines;
- register long length,
- totlength;
- char **newtext,
- newline[256];
- register TEHandle hTE = (**hFD).TE;
-
- SetHandleSize( hFD, (long) sizeof( FDRec ) + sizeof( long ) * max );
- if ( !memok() ) return;
-
- newtext = (char **) NewHandle( 0L );
- if ( !memok() ) return;
- totlength = 0L;
-
- for ( i = 0; i < max; ++i, offset = 1L ) {
- LineFile( LF_GetLine, &(**hFD).file, pos, offset, newline, &pos );
- length = strlen( newline );
-
- if ( length == 0 )
- if ( i == 0 && offset < 0 ) {
- LineFile( LF_GetLine, &(**hFD).file, 0L, 0L, newline, &pos );
- if ( ( length = strlen( newline ) ) == 0 )
- break;
- }
- else
- break;
-
- (**hFD).filestarts[ i ] = pos;
-
- SetHandleSize( newtext, totlength + length );
- if ( !memok() ) {
- DisposHandle( newtext );
- return;
- }
-
- BlockMove( newline, *newtext + totlength, length );
- totlength += length;
- }
-
- if ( totlength != 0 ) {
- if ( (**hTE).hText != NULL )
- DisposHandle( (**hTE).hText );
- (**hTE).hText = newtext;
- TECalText( hTE );
- (**hFD).actuallines = i;
- set_te_select( hFD );
- }
- }
-
- static void New( phFD, lfile, viewrect, tsize )
- register FDHandle *phFD;
- LFile *lfile;
- Rect *viewrect;
- long tsize;
- {
- register FDPtr pFD;
- Rect destrect;
-
- *phFD = (FDHandle) NewHandle( (long) sizeof( FDRec ) );
- if ( !memok() ) return;
-
- HLock( *phFD );
- pFD = **phFD;
-
- destrect = *viewrect;
- destrect.left += BORDER;
- destrect.right = 20000;
-
- if ( ( pFD->TE = TENew( &destrect, viewrect ) ) == NULL ) {
- HUnlock( *phFD );
- DisposHandle( *phFD );
- *phFD = NULL;
- return;
- }
-
- (**pFD->TE).crOnly = -1;
-
- pFD->tabsize = tsize;
- pFD->file = *lfile;
-
- pFD->sel_start = pFD->sel_end = 0L;
-
- pFD->maxlines = MAXLINES( pFD->TE );
- (**pFD->TE).viewRect.bottom =
- viewrect->top + pFD->maxlines * (**pFD->TE).lineHeight;
-
- HUnlock( *phFD );
-
- FillFD( *phFD, 0L, 0L );
- }
-
- static void Redraw( hFD, startpos, offset )
- register FDHandle hFD;
- register long *startpos;
- long offset;
- {
- FillFD( hFD, *startpos, offset );
- if ( (**hFD).actuallines > 0 )
- *startpos = (**hFD).filestarts[ 0 ];
- else
- *startpos = 0L;
-
- Update( hFD );
- Activate( hFD, 1L );
- }
-
- static void Scroll( hFD, dh, dv, startpos )
- register FDHandle hFD;
- register long dv;
- long dh, *startpos;
- {
- char newline[ 256 ];
- register long totlength,
- newlength,
- lengthremoved;
- long pos,
- newlinestart;
- int max = (**hFD).maxlines;
- TEHandle hTE = (**hFD).TE;
- register Handle thetext = (**hTE).hText;
- RgnHandle updatergn,
- oldclip;
- Rect srect;
- GrafPtr oldport;
-
- GetPort( &oldport );
- SetPort( (**hTE).inPort );
- oldclip = NewRgn();
- GetClip( oldclip );
-
- *startpos = (**hFD).filestarts[ 0 ];
-
- if ( dv != 0 ) {
- dv = ( dv < 0 ) ? -1L : 1L;
-
- pos = ( (**hFD).actuallines == 0 ) ?
- 0L
- : ( dv > 0 ) ?
- (**hFD).filestarts[ 0 ]
- : (**hFD).filestarts[ (**hFD).actuallines - 1 ];
-
- LineFile( LF_GetLine, &(**hFD).file, pos, -dv,
- newline, &newlinestart );
-
- newlength = strlen( newline );
- totlength = (**hTE).teLength;
-
- if ( newlength != 0 ) {
- lengthremoved =
- ( (**hFD).actuallines < max ) ?
- 0L
- : ( dv > 0 ) ?
- totlength - (**hTE).lineStarts[ max - 1 ]
- : ( max < 2 ) ?
- totlength
- : (**hTE).lineStarts[ 1 ];
-
- SetHandleSize( thetext, totlength + newlength );
- if ( !memok() ) return;
- totlength -= lengthremoved;
-
- if ( dv > 0 ) {
- BlockMove( *thetext, *thetext + newlength, totlength );
- BlockMove( newline, *thetext, newlength );
- BlockMove( (**hFD).filestarts, (**hFD).filestarts + 1,
- (long) sizeof( long ) * ( max - 1 ) );
- if ( (**hFD).actuallines < max )
- ++(**hFD).actuallines;
- (**hFD).filestarts[ 0 ] = newlinestart;
- }
- else {
- BlockMove( *thetext + lengthremoved, *thetext, totlength );
- BlockMove( newline, *thetext + totlength, newlength );
- BlockMove( (**hFD).filestarts + 1, (**hFD).filestarts,
- (long) sizeof( long ) * ( max - 1 ) );
- (**hFD).filestarts[ max - 1 ] = newlinestart;
- }
-
- SetHandleSize( thetext, totlength + newlength );
- TECalText( hTE );
- set_te_select( hFD );
- *startpos = (**hFD).filestarts[ 0 ];
- }
-
- else /* newlength == 0 */
- dv = 0L;
- }
-
- if ( dv != 0L || dh != 0L ) {
- srect = (**hTE).viewRect;
- updatergn = NewRgn();
- ScrollRect( &srect, (int) dh, (int) dv * (**hTE).lineHeight, updatergn );
-
- OffsetRect( &(**hTE).destRect, (int) dh, 0 );
-
- SetClip( updatergn );
- srect = (**updatergn).rgnBBox;
- TEUpdate( &srect, hTE );
- Activate( hFD, 1L );
- DisposeRgn( updatergn );
- }
-
- SetClip( oldclip );
- SetPort( oldport );
-
- DisposeRgn( oldclip );
- }
-
- static void Resize( hFD, viewrect )
- register FDHandle hFD;
- Rect *viewrect;
- {
- register TEHandle hTE = (**hFD).TE;
- int dest_offset;
-
- dest_offset = (**hTE).destRect.left - (**hTE).viewRect.left;
-
- (**hTE).destRect = (**hTE).viewRect = *viewrect;
- (**hTE).destRect.left += dest_offset;
- (**hTE).destRect.right = 20000;
-
- (**hFD).maxlines = MAXLINES( hTE );
- (**hTE).viewRect.bottom = viewrect->top
- + (**hFD).maxlines * (**hTE).lineHeight;
-
- FillFD( hFD, (**hFD).filestarts[ 0 ], 0L );
- Activate( hFD, 1L );
- }
-
- static void SetTabs( hFD, size )
- register FDHandle hFD;
- long size;
- {
- register TEHandle hTE = (**hFD).TE;
- GrafPtr oldport,
- port = (**hTE).inPort;
- Rect vrect;
-
- GetPort( &oldport );
- SetPort( port );
-
- (**hFD).tabsize = size;
-
- vrect = (**hTE).viewRect;
- InvalRect( &vrect );
-
- SetPort( oldport );
- }
-
- static void Update( hFD )
- FDHandle hFD;
- {
- Rect updaterect;
- register TEHandle hTE = (**hFD).TE;
-
- updaterect = (**hTE).viewRect;
- EraseRect( &updaterect );
- TEUpdate( &updaterect, hTE );
- }
-
- pascal void tabs_textproc( size, text, numer, denom )
- int size;
- char *text;
- Point numer, denom;
- {
- int newsize;
- char *newtext;
-
- SetUpA4();
-
- convertabs( size, text, &newsize, &newtext );
-
- StdText( newsize, newtext, numer, denom );
-
- DisposPtr( newtext );
-
- RestoreA4();
- }
-
- pascal int tabs_txmeasproc( size, text, numer, denom, info )
- int size;
- char *text;
- Point *numer, *denom;
- Ptr info;
- {
- int newsize;
- register int length;
- char *newtext;
-
- SetUpA4();
-
- convertabs( size, text, &newsize, &newtext );
-
- length = StdTxMeas( newsize, newtext, numer, denom, info );
-
- DisposPtr( newtext );
-
- RestoreA4();
-
- return length;
- }
-
- convertabs( oldsize, oldtext, newsize, newtext )
- int oldsize, *newsize;
- register char oldtext[], **newtext;
- {
- register int i, j, numtabs, stop;
-
- for ( i = 0, numtabs = 0; i < oldsize; ++i )
- if ( oldtext[ i ] == '\t' ) ++numtabs;
-
- *newsize = oldsize + numtabs * ( tab_size - 1 );
-
- *newtext = (char *) NewPtr( (long) *newsize );
-
- for ( i = 0, j = 0; i < oldsize; ++i )
- if ( oldtext[ i ] == '\t' ) {
- stop = ( j / tab_size + 1 ) * tab_size;
- for ( ; j < stop; ++j )
- (*newtext)[ j ] = ' ';
- }
- else
- (*newtext)[ j++ ] = oldtext[ i ];
-
- *newsize = j;
- }
-
- patchtabs( hFD )
- FDHandle hFD;
- {
- SetStdProcs( &lowqdprocs );
- lowqdprocs.textProc = ( QDPtr ) tabs_textproc;
- lowqdprocs.txMeasProc = ( QDPtr ) tabs_txmeasproc;
-
- (**(**hFD).TE).inPort->grafProcs = &lowqdprocs;
- tab_size = (**hFD).tabsize;
- }
-
- unpatchtabs( hFD )
- FDHandle hFD;
- {
- (**(**hFD).TE).inPort->grafProcs = NULL;
- }
-
- static void Click( hFD, theEvent )
- register FDHandle hFD;
- EventRecord *theEvent;
- {
- Point where;
- register TEHandle hTE = (**hFD).TE;
- Boolean extendp;
- long start, length;
-
- theFD = hFD;
-
- extendp = ( ( theEvent->modifiers & shiftKey ) != 0 );
-
- where = theEvent->where;
- GlobalToLocal( &where );
-
- TEActivate( hTE );
- TEClick( where, extendp, hTE );
-
- start = (**hFD).filestarts[ 0 ];
- length = (**hTE).teLength;
-
- if ( !extendp || (**hFD).sel_start >= start )
- (**hFD).sel_start = start + (**hTE).selStart;
- if ( !extendp || (**hTE).selEnd < length || (**hFD).sel_end < length )
- (**hFD).sel_end = start + (**hTE).selEnd;
- }
-
- static void Dispose( hFD )
- register FDHandle hFD;
- {
- if ( hFD != NULL ) {
- TEDispose( (**hFD).TE );
- HLock( hFD );
- LineFile( LF_Close, &(**hFD).file );
- HUnlock( hFD );
- DisposHandle( hFD );
- }
- }
-
- static void Copy( hFD )
- register FDHandle hFD;
- {
- int err;
- char errs[256];
- register Ptr temp_scrap;
- long length;
-
- if ( ( length = (**hFD).sel_end - (**hFD).sel_start ) == 0L ) return;
-
- temp_scrap = NewPtr( length );
- if ( !memok() ) return;
-
- err = SetFPos( (**hFD).file.refnum, fsFromStart, (**hFD).sel_start );
- if ( err != noErr ) {
- NumToString( (long) err, errs );
- GenError( "\pError writing to Clipboard: ", errs, "\p", "\p" );
- }
-
- err = FSRead( (**hFD).file.refnum, &length, temp_scrap );
- if ( err != noErr && err != eofErr ) {
- NumToString( (long) err, errs );
- GenError( "\pError writing to Clipboard: ", errs, "\p", "\p" );
- }
-
- ZeroScrap();
- if ( ( err = PutScrap( length, 'TEXT', temp_scrap ) ) != noErr ) {
- NumToString( (long) err, errs );
- GenError( "\pError writing to Clipboard: ", errs, "\p", "\p" );
- }
-
- DisposPtr( temp_scrap );
- }
-
- static void Activate( hFD, which )
- register FDHandle hFD;
- long which;
- {
- if ( which != 0 && sel_in_view( hFD ) )
- TEActivate( (**hFD).TE );
- else
- TEDeactivate( (**hFD).TE );
- }
-
- static void Idle( hFD )
- FDHandle hFD;
- {
- TEIdle( (**hFD).TE );
- }
-